define(function(require) {
    var ko = require("knockout");
    var router = require("plugins/router");
    var $ = require("jquery");
    require("jqueryCookie");
    var UpdateAccountTemp = require("text!./update-account-temp.html");

    var transformFormData = function(data) {
        var newData = { };
        for (var pro in data) {
            if ((pro === "isFinish") || (pro === "isRunning")) {
                if (data[pro] === "true") {
                    newData[pro] = true;
                } else if (data[pro] === "false") {
                    newData[pro] = true;
                }
            } else if (pro === "id") {
                newData[pro] = parseInt(data[pro]);
            } else {
                newData[pro] = data[pro];
            }
        }
        return newData;
    };

    //当前的用户 id
    var modelId = null;

    return {
        activate: function(id) {
            modelId = id;
        },
        compositionComplete: function() {
            layui.use(["laytpl", "table", "element", "layer", "form"], function() {
                var laytpl = layui.laytpl;
                var form = layui.form;
                var layer = layui.layer;
                var element = layui.element;

                form.on("submit(update-submit)", function(data) {
                    $.ajax({
                        type: "PUT",
                        url: "/back/plan",
                        dataType: "json",
                        contentType: "application/json",
                        data: JSON.stringify(transformFormData(data.field))
                    }).then(function(response) {
                        layer.alert(response.msg, function(index) {
                            layer.close(index);
                            router.navigate("plan");
                        });
                    }, function(response) {
                        layer.alert(response.responseJSON.msg);
                    });
                    return false;
                });

                $.ajax({
                    type: "get",
                    url: "/back/plan/" + modelId,
                    dataType: "json",
                    contentType: "application/json"
                }).then(function(response) {
                    laytpl(UpdateAccountTemp).render({ data: response.data }, function(html) {
                        $("#update-content").html(html);
                        form.render();
                        //修改用户信息界面返回按钮，用以取消编辑返回列表界面
                        $("#update-back").click(function() {
                            router.navigate("plan/backAccountSelect");
                            return false;
                        });
                    });
                }, function(data) {
                    layer.alert(data.responseJSON.msg);
                });
                /*$.when(Services.accountById(modelId), Services.roles()).then(function(account, roles) {
                    var isChecked = function(authId) {
                        var index = $.inArray(authId, account.roleIds);
                        var isExists = false;
                        if (index >= 0) {
                            isExists = true;
                        }
                        return isExists;
                    };
                    laytpl(UpdateAccountTemp).render({ account: account.account, roles: roles, isChecked: isChecked }, function(html) {
                        $("#update-content").html(html);
                        form.render();
                        //修改用户信息界面返回按钮，用以取消编辑返回列表界面
                        $("#update-back").click(function() {
                            router.navigate("security/backAccountSelect");
                            return false;
                        });
                    });
                });*/
            });
        }
    };
});